//
//  SQLiteTool.swift
//  rryproject
//
//  Created by zhengyi on 2018/3/1.
//  Copyright © 2018年 inesv. All rights reserved.
//

import Foundation
import SQLite

class SQLiteManager: NSObject {
    
    static let shareInstance: SQLiteManager = SQLiteManager()
    
    private var db: Connection!
    
    private var table = Table("db_rry")
    
    private var walletAddress = Expression<String>("walletAddress")
    private var walletName = Expression<String>("walletName")
    private var walletPwd = Expression<String>("walletPwd")
    private var walletPrice = Expression<Double>("walletPrice")
    
    func connectCreatTalbe(dbName: String) {
        let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
        let filePath = path?.appending("/"+dbName)
        creatTable(filePath!)
    }
    
    private func creatTable(_ filePath: String) {
        
        do{
            db = try Connection(filePath)
            
            try db.run(table.create(ifNotExists:true){ t in
                t.column(walletAddress, primaryKey: true)//主键
                t.column(walletName)
                t.column(walletPwd)//, unique: true)//非空
                t.column(walletPrice)
            })
            Tools.DLog(message: "建表成功------")
        }catch{ Tools.DLog(message: error) }
        
    }
    
    func insertData(_walletAddress: String, _walletName: String, _walletPwd: String, _walletPrice: Double) {
        do{
            let insert = table.insert(walletAddress <- String(_walletAddress), walletName <- String(_walletName), walletPwd <- String(_walletPwd), walletPrice <- Double(_walletPrice))
            try db.run(insert)
            Tools.DLog(message: "数据插入成功")
        }catch{
            Tools.DLog(message: error)
        }
    }
    
    func readData(_walletAddress: String, _walletName: String, _walletPwd: String, _walletPrice: Double) {
        do{
            let insert = table.insert(walletAddress <- String(_walletAddress), walletName <- String(_walletName), walletPwd <- String(_walletPwd), walletPrice <- Double(_walletPrice))
            try db.run(insert)
            Tools.DLog(message: "数据插入成功")
        }catch{
            Tools.DLog(message: error)
        }
    }
    
   
    
    func qureyData(addres: String) -> Bool {
        
        let filter = table.filter(walletAddress == addres)
        var count = 0
        for _ in (try! db.prepare(filter)) {
            count += 1
        }
        if  count > 0 {
            return true
        } else {
            return false
        }
    }
    
    func updateName(name: String, address: String) {
        
        let filter = table.filter(walletAddress == address)
        do{
            if try! db.run(filter.update(walletName <- name)) > 0 {
                Tools.DLog(message: "更新表成功")
            } else {
                Tools.DLog(message: "没有发现该钱包")
            }
        }catch{
            Tools.DLog(message: "更新表失败")
        }
        
    }
    
    func updatePrice(price: Double, address: String) {
        
        let filter = table.filter(walletAddress == address)
        do{
            if try! db.run(filter.update(walletPrice <- price)) > 0 {
                Tools.DLog(message: "更新表成功")
            } else {
                Tools.DLog(message: "没有发现该钱包")
            }
        }catch{
            Tools.DLog(message: "更新表失败")
        }
    }
    
    func queryTalbe() {
        for item in (try! db.prepare(table)) {
            Tools.DLog(message: "\n读取钱包： address:\(item[walletAddress]), name: \(item[walletName]), price:\(item[walletPrice])")
        }
    }
    
    func getWalletArrayInDB() -> NSMutableArray {
        let array = NSMutableArray()
        for item in (try! db.prepare(table)) {
            let wallet = WalletAddress()
            wallet.address = item[walletAddress]
            wallet.name = item[walletName]
            wallet.password = item[walletPwd]
//            wallet.price = item[walletPrice]
            array.add(wallet)
        }
        return array
    }
    
    func getWalletAddressStringInDB() -> String {
        var array = ""
        for item in (try! db.prepare(table)) {
            let address = item[walletAddress]
            array = array + address + ","
        }
        if array.count > 0 {
            array.removeLast()
        }
        return array
    }
}
